﻿--[[
    @brief     ui logic for startup_speedup
    @author    jameshchen
    @version   1.0
    @date      2015-03-17
--]]

startup_speedup_control = 
{
    init = function()
        -- body
        base_utility.log("startup_speedup_control.init called");
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        ierd_tgp.tpf_ui.Require("tool_box\\model.lua");
        event_center.add_listener(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_ST, 
            'startup_speedup_control.on_get_js_callback');
        tpf_ui:AddLuaListener(ierd_tgp.preload.PRELOAD_ON_PRELOAD_ENABLE_CHANGED,
            "startup_speedup_control.on_game_enable_change");
    end;

    on_get_js_callback = function(event_id, jsonp)
        -- body
        local data_table = base_utility.json_to_table(jsonp);
        local fuc_name = data_table["fuc_name"];
        local str = base_utility.json_to_table(data_table["json"]);

        if fuc_name == "st_preload_operation" then
            startup_speedup_control.handle_preload_operation(str);
        elseif fuc_name == "st_open_inner_link" then
            startup_speedup_control.open_game_detail_page(str);
        else 
            base_utility.log("some other startup_speedup_control fun_name"..fuc_name);
        end;
    end;

    handle_preload_operation = function(paramTable)
        -- get the parameters as the form of table
        -- local paramTable = base_utility.json_to_table(const_ui_str);
        if paramTable.op == "get_preload_game_list" then
            startup_speedup_control.get_preload_game_list();
        elseif paramTable.op == "enable_preload" then
            startup_speedup_control.enable_preload(paramTable);
        elseif paramTable.op == "install_game" then
            startup_speedup_control.install_game(paramTable);
        else 
            base_utility.log("preload_operation must have something wrong");
        end;
    end;
    
    -- todo
    get_preload_game_list = function()
        -- local json = "set_network_acc_base_info";
        -- -- 分发结构调整
        -- local fuc_name = "st_preload_event";
        local preload =
            base_utility.get_component_adapter("IPreload_mgr",
                "ierd_tgp.preload.GetPreloadMgrAdapter();");
        local enabled;
        local want_pack_var;
        if preload ~= nil then
            local game_list_temp = ierd_tgp.preload.Preload_game_adapter_list:new();
            preload:get_game_list(game_list_temp);
            local should_preload = preload:ShouldPreload();
            local game_list_table = {event = game_list_temp.event,
            count = game_list_temp.list_count, should_preload = should_preload};
            game_list_table.list = {};
            local itemCount = game_list_temp.list_count;
            local count = 0;
            while(count < itemCount) do
                local game_id_tmp = game_list_temp.game_list[count].game_id;
                local support_pack_tmp = game_list_temp.game_list[count].support_pack;
                if game_id_tmp == 26 or game_id_tmp == 10026 then
                    support_pack_tmp = false;  -- 英雄联盟不支持深度优化
                end;
                enabled = game_list_temp.game_list[count].enabled;
                want_pack_var = game_list_temp.game_list[count].want_pack;
                if banner_model.is_showed_in_banner_list(game_id_tmp) then
                    table.insert(game_list_table, {
                        game_id = game_id_tmp,
                        name = game_list_temp.game_list[count].name,
                        e_name = game_list_temp.game_list[count].e_name,
                        icon_url = game_list_temp.game_list[count].icon,
                        installed = game_list_temp.game_list[count].installed,
                        on = enabled,
                        support_pack = support_pack_tmp,
                        support_map_preload = game_list_temp.game_list[count].support_map_preload,
                        need_pack = game_list_temp.game_list[count].need_pack,
                        need_update = game_list_temp.game_list[count].need_update,
                        want_pack = want_pack_var
                    });

                    tool_box_model.game_id_to_enable_state[game_id_tmp] = enabled;
                    tool_box_model.game_id_to_want_pack_state[game_id_tmp] = want_pack_var;
                else
                    base_utility.log("[get_preload_game_list]not visible, " ..
                            tostring(game_id_tmp));
                end
                count = count + 1;
            end;
            local game_list_json = base_utility.table_to_json(game_list_table);
            web_utility.lua_call_js("st_preload_event", game_list_json, CID_TOOLBOX_QBWEB);
            game_list_temp:delete();
        end;
    end;

    enable_preload = function(paramTable)
        local preload =
            base_utility.get_component_adapter("IPreload_mgr",
                "ierd_tgp.preload.GetPreloadMgrAdapter();");
        local game_id = paramTable.game_id;
        local enable_preload = paramTable.enabled;
        local want_pack = paramTable.want_pack;
        local from_webui = true;

        local enable_str = 'true';
        local want_pack_str = 'true';

        if enable_preload ~= true then
            enable_str = 'false'
        end

        if want_pack ~= true then
            want_pack_str = 'false'
        end

        tool_box_model.game_id_to_enable_state[game_id] = enable_preload;
        tool_box_model.game_id_to_want_pack_state[game_id] = want_pack;
        base_utility.log("enable_preload [" .. game_id 
            .. "], ["..enable_str.."],["..want_pack_str.."]");
        preload:PackEnablePreload(game_id, enable_preload, want_pack, from_webui);

        if want_pack then
            startup_speedup_control.enable_pack(game_id);
        end
    end;

    on_preload_close = function(event_id, json_str)
        base_utility.log("on_preload_close, called");
        local param = base_utility.json_to_table(json_str);
        if param == nil then
            return;
        end

        local preload =
            base_utility.get_component_adapter("IPreload_mgr",
                "ierd_tgp.preload.GetPreloadMgrAdapter();");
        if preload == nil then
            return;
        end

        local game_id = param.game_id;

        local game_preload_temp = ierd_tgp.preload.Preload_game_adapter:new();
        local success = preload:GetGamePreloadInfo(game_id, game_preload_temp);

        if success ~= true then
            return;
        end

        local new_param = {
            game_id = game_id,
            need_pack = game_preload_temp.need_pack
        };

        local json_param = base_utility.table_to_json(new_param);
        base_utility.log("on_preload_close, game_id=".. param.game_id);

        base_utility.log("[on_preload_close] lua_call_js, param="..json_param);
        web_utility.lua_call_js('st_preload_close', json_param, CID_TOOLBOX_QBWEB);
        game_preload_temp:delete();
    end;


    install_game = function(paramTable)
        local game_id = paramTable.game_id;
        local str = {};
        str.type = "mmo_game";
        str.sub_page = "GAME_DETAIL";
        str.game_id = game_id;
        web_utility.open_link(str);
    end;

    open_game_detail_page = function(str)
        -- body
        local top_window = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        if top_window ~= NULL and top_window:IsVisible() == true then
            top_window:BringToTop();
        else
            if game_lib ~= NULL then
                game_lib:notify_show_main_wnd();
            end;
        end;
        
        local param = base_utility.table_to_json(str);
        web_utility.open_link(param);
    end;

    on_game_enable_change = function(event_id, json)
        -- body
        local param = base_utility.json_to_table(json);
        local game_id = param.game_id;
        local enabled = param.enabled;
        if tool_box_model.game_id_to_enable_state[game_id] ~= enabled then
            -- refresh page
            web_utility.lua_call_js('st_refresh',json, CID_TOOLBOX_QBWEB);
            base_utility.log("startup mode change by other, refresh toolbox");
        else
            base_utility.log("工具箱自己改变游戏深度优化状态，无需处理!");
        end;
    end;

    enable_pack = function(game_id)
        local server_id = 0;
        local param = {
            game_id = game_id,
            server_id = server_id
        };
        base_utility.log("show the pack window!, game_id="..game_id);

        local json = base_utility.table_to_json(param);
        event_center.send_event(preloadpage_event.PRELOAD_LOAD_PAGE_SUCCESS, json);
    end;
};

startup_speedup_control.init();
